package com.xuexi.controller;

import com.easygo.utils.MessageResults;
import com.easygo.utils.PageUtils;
import com.xuexi.api.ContentCategoryClient;
import com.xuexi.easygo.pojo.ContentCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Scope("prototype")
public class ContentCategoryController {

    @Autowired
    ContentCategoryClient categoryClient;

    @RequestMapping("/contentCategory_page")
    public String getcontentCategorys(@RequestParam(defaultValue = "1",required = false,value = "pageIndex") Integer pageIndex,
                                                          @RequestParam(defaultValue = "5",required = false,value = "pageSize") Integer pageSize,
                                                          @RequestParam(defaultValue = "",required = false,value = "name") String name,
    Model model){
        PageUtils<ContentCategory> pageUtils = categoryClient.getcontentCategorys(pageIndex, pageSize, name);
        model.addAttribute("pageUtils",pageUtils);
        //回显搜索条件
        model.addAttribute("name",name);
        return "content_category";
    }

    @RequestMapping("contentCategory_add")
    @ResponseBody
    public MessageResults contentCategory_add(ContentCategory category){
        int count = categoryClient.addContentCategory(category);
        MessageResults results = null;
        if (count > 0) {
            results = new MessageResults(200, "新增成功");
        } else {
            results = new MessageResults(500, "新增失败");
        }
        return results;
    }

    @RequestMapping("contentCategory_getById")
    @ResponseBody
    public ContentCategory contentCategory_getById(Integer id){
        return categoryClient.getById(id);
    }

    @RequestMapping("contentCategory_update")
    @ResponseBody
    public MessageResults contentCategory_update(ContentCategory category){
        MessageResults results = null;
        int count = categoryClient.updateContentCategory(category);
        if(count>0){
            results = new MessageResults(200,"更新成功");
        }else {
            results = new MessageResults(500,"更新失败");
        }
        return results;
    }

    //下拉框数据
    @RequestMapping("contentCategory_list")
    @ResponseBody
    public List<ContentCategory> getContentCategorys(){
        return categoryClient.getContentCategorys();
    }
}
